import React from 'react';
import { Radio } from 'antd';
import { isFunction } from '../../../utils/is-object';

export default function createRadio(config = {}) {
  const {
    property,
    data = {},
    size = 'small',
    onChange,
    getter = (data, property) => data[property],
    getValue = value => value,
    options = []
  } = config;
  const onChangeHandler = event => {
    let { value } = event.target;
    if (isFunction(getValue)) {
      value = getValue(value);
    }
    if (isFunction(onChange)) {
      onChange(value, property);
    }
  };
  const value = getter(data, property);

  return (
    <Radio.Group
      value={value}
      onChange={onChangeHandler}
      size={size}
      options={options}
    />
  );
}
